package com.example.qt.ptplatform.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.qt.ptplatform.entity.InviteCode;
import com.example.qt.ptplatform.entity.User;
import com.example.qt.ptplatform.mapper.InviteCodeMapper;
import com.example.qt.ptplatform.mapper.UserMapper;
import com.example.qt.ptplatform.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
public class InviteCodeServiceImpl extends ServiceImpl<InviteCodeMapper, InviteCode> implements com.example.qt.ptplatform.service.InviteCodeService {

    @Autowired
    private InviteCodeMapper inviteCodeMapper;

    @Autowired
    private UserMapper userMapper;

    // 生成邀请码
    public JsonResult generateInviteCode(Integer userId) {
        User user = userMapper.selectById(userId);
        if (user == null) {
            return JsonResult.error("用户不存在");
        }

        int userLevel = user.getLevel();
        int generatedCountThisMonth = inviteCodeMapper.countInviteCodesByUserIdAndMonth(userId, getCurrentMonthYear());
        int maxGenerateCount = getMaxInviteCountByLevel(user);

        // 判断该用户在当前月生成的邀请码数量是否超过限制
        if (generatedCountThisMonth >= maxGenerateCount) {
            return JsonResult.error("已达到每月生成的最大邀请码数量");
        }

        // 生成一个新的邀请码
        String inviteCode = generateRandomInviteCode();
        InviteCode code = new InviteCode();
        code.setCode(inviteCode);
        code.setUserId(userId);
        code.setCreatedAt(new Date());
        code.setExpiration(new Date(System.currentTimeMillis() + 30 * 60 * 1000));  // 设置30分钟后过期
        code.setMonthYear(getCurrentMonthYear()); // 设置当前年月

        inviteCodeMapper.insert(code);

        int inviteCount=user.getInviteCount();

        return JsonResult.success(code);
    }

    // 获取当前年月，格式为：YYYY-MM
    private String getCurrentMonthYear() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        return sdf.format(new Date());
    }

    private int getMaxInviteCountByLevel(User user) {
        int inviteCount=user.getInviteCount();
        return inviteCount;
    }

    private String generateRandomInviteCode() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    // 验证邀请码
    public String validateInviteCode(String inviteCode) {
        InviteCode code = inviteCodeMapper.selectByCode(inviteCode);

        if (code == null) {
            //return JsonResult.error("邀请码不存在");
            return "邀请码不存在";
        }

        if (code.getIsUsed()) {
            //return JsonResult.error("邀请码已经被使用");
            return "邀请码已经被使用";
        }

        if (code.getExpiration().before(new Date())) {
            //return JsonResult.error("邀请码已过期");
            return "邀请码已过期";
        }

        // 如果邀请码有效，标记为已使用
        code.setIsUsed(true);
        code.setUsedAt(new Date());
        inviteCodeMapper.updateById(code);

        //return JsonResult.success("邀请码验证成功");
        return "邀请码验证成功";
    }
}

